﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Archive.Data;
//using CentralOffice.Archive.DataAccess;
using CentralOffice.Archive.BusinessActions;

using ArchiveTable = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesDataTable;
using ArchiveRow = CentralOffice.Archive.Data.ArchiveDataSet.ArchivesRow;

namespace CentralOffice.WebClient.Archives
{
    public partial class ArchiveQuery : PageBase
    {
        private bool? authorized = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //SiteUtil.PopulateListBox(ddlPriority, SiteConstants.TaskPriority, true);
                //ddlPriority.SelectedIndex = 0;

                //SiteUtil.PopulateListBox(ddlStatus, SiteConstants.TaskStatus, true);
                SiteUtil.PopulateListBox(ddlSecretLevel, SiteConstants.SecretLevel, false);

                //bool secretLevel = this.Authorize(AuthorizationRules.Archive_TopSecret);
                bool secretLevel1 = this.Authorize(AuthorizationRules.Archive_Public);
                bool secretLevel2 = this.Authorize(AuthorizationRules.Archive_Common);
                if (secretLevel2)
                {
                    ddlSecretLevel.Items.RemoveAt(2);
                }

                if (secretLevel1 && !secretLevel2)
                {
                    if (ddlSecretLevel.Items.Count > 2)
                    {
                        ddlSecretLevel.Items.RemoveAt(2);
                    }
                    if (ddlSecretLevel.Items.Count > 1)
                    {
                        ddlSecretLevel.Items.RemoveAt(1);
                    }
                }
            }

            if (authorized == null)
            {
                authorized = this.Authorize(AuthorizationRules.Archive_Update);
            }
            if (!authorized.Value)
            {
                gv.Columns.RemoveAt(gv.Columns.Count - 1);
                gv.Columns.RemoveAt(gv.Columns.Count - 1);
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.PageIndex = 0;

            if (txtSearchText.Text.Trim().Length > 0)
            {
                odsArchive.SelectParameters.Clear();
                odsArchive.SelectParameters.Add("whereClause", GetWhereCondition(txtSearchText.Text.Trim()));
                return;
            }

            ArchiveRow archive = (new ArchiveDataSet()).Archives.NewArchivesRow();

            archive.Name = txtName.Text;
            if (ddlCategory.SelectedIndex > 0)
            {
                archive.CategoryId = new Guid(ddlCategory.SelectedValue);

                // archive.Category = ddlCategory.SelectedItem.Text;
            }
            //archive.Name = txtName.Text;
            archive.ContractCode = txtContractCode.Text;
            archive.QueryNumber = CommonUtil.GetChineseSpell(txtName.Text);
            archive.SecretLevel = ddlSecretLevel.SelectedItem.Text;
            archive.Province = txtProvince.Value;
            archive.City = txtCity.Value;
            //archive.County = txtCounty.Text;
            archive.ContractCode = txtContractCode.Text;
            if(txtFiledDateStart.Text.Length > 0)
            {
                archive.FiledDate = DateTime.Parse(txtFiledDateStart.Text);
            }
            if (txtFiledDateEnd.Text.Length > 0)
            {
                archive.FiledDateEnd = DateTime.Parse(txtFiledDateEnd.Text);
            }
            
           
            //archive.CustomerId = txtCustomerId.Text;
            archive.CategoryId = new Guid(ddlCategory.SelectedValue);
            if (txtScale.Text.Length > 0)
            {
                archive.Scale = CommonUtil.IsInteger(txtScale.Text) ? Int32.Parse(txtScale.Text) : -1;
            }
            archive.ConstructForm = txtConstructForm.Text;
            archive.DesignStage = txtDesignStage.Text;
            archive.BelongedTo = txtBelongedTo.Text;
            archive.ProjectManager = txtProjectManager.Text;            

            archive.Customer = txtCustomer.Text;
            //archive.QueryNumber = txtQueryNumber.Text;
            archive.Address = txtAddress.Text;
            archive.Telephone1 = txtTelephone1.Text;
            //archive.Telephone2 = txtTelephone2.Text;
            //archive.Fax = txtFax.Text;
            //archive.Email = txtEmail.Text;
            archive.Contact = txtContact.Text;

            //task.CloseDate = txtCloseDate.Text;
            //task.Profile = txtProfile.Text;

            odsArchive.SelectParameters.Clear();
            odsArchive.SelectParameters.Add("whereClause", archive.FilterExpression);
        }

        protected void odsArchive_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (!e.ExecutingSelectCount)
            {
                //e.Arguments
            }
            if (this.IsPostBack && e.InputParameters["whereClause"] == null)
            {
            }

            if (e.InputParameters["whereClause"] == null || e.InputParameters["whereClause"].ToString() == "")
            {
                ArchiveRow archive = (new ArchiveDataSet()).Archives.NewArchivesRow();
                string where = " AND  SecretLevel = '公开'";

                bool secretLevel = this.Authorize(AuthorizationRules.Archive_Common);
                //secretLevel = this.Authorize(AuthorizationRules.Archive_Common);
                if (secretLevel)
                {
                    where = " AND ( SecretLevel = '普密' OR SecretLevel = '公开')";
                }
                secretLevel = this.Authorize(AuthorizationRules.Archive_TopSecret);
                if (secretLevel)
                {
                    where = " AND (SecretLevel = '绝密' OR SecretLevel = '普密' OR SecretLevel = '公开')";
                }
                
                e.InputParameters["whereClause"] = where;
            }
        }

        public string GetWhereCondition(string criteria)
        {
            if (string.IsNullOrEmpty(criteria))
            {
                return "";
            }
            string where = " ( Name LIKE '%" + criteria + "%' OR ";
            where += "SecretLevel LIKE '%" + criteria + "%' OR ";
            where += "Province LIKE '%" + criteria + "%' OR ";
            where += "City LIKE '%" + criteria + "%' OR ";
            where += "County LIKE '%" + criteria + "%' OR ";
            where += "ContractCode LIKE '%" + criteria + "%' OR ";
            where += "ConstructForm LIKE '%" + criteria + "%' OR ";
            where += "DesignStage LIKE '%" + criteria + "%' OR ";
            where += "BelongedTo LIKE '%" + criteria + "%' OR ";
            where += "ProjectManager LIKE '%" + criteria + "%' OR ";
            where += "BuildingBlueprint LIKE '%" + criteria + "%' OR ";
            where += "ElectricalBlueprint LIKE '%" + criteria + "%' OR ";
            where += "MechanicalMaterialManifest LIKE '%" + criteria + "%' OR ";
            where += "ElectricalMaterialManifest LIKE '%" + criteria + "%' OR ";
            where += "SystemSolution LIKE '%" + criteria + "%' OR ";
            where += "ElectricalSolution LIKE '%" + criteria + "%' OR ";
            where += "TenderDocumentation LIKE '%" + criteria + "%' OR ";
            where += "PreliminaryDesign LIKE '%" + criteria + "%' OR ";
            where += "OtherDocumentation LIKE '%" + criteria + "%' OR ";
            where += "KeyWords LIKE '%" + criteria + "%' OR ";
            where += "Customer LIKE '%" + criteria + "%' OR ";
            where += "Address LIKE '%" + criteria + "%' OR ";
            where += "Telephone1 LIKE '%" + criteria + "%' OR ";
            where += "Contact LIKE '%" + criteria + "%' OR ";
            where += "ContactPhone LIKE '%" + criteria + "%' OR ";
            where += "Category LIKE '%" + criteria + "%' ) ";

            return where;
        }
    }
}
